package com.efuture.mutlithread;

/**
 * @ClassNAME DealLock
 * @Description 死锁
 * @Author zhaoshanshan
 * @Date 2021/9/11 12:08 下午
 * @Version 1.0
 */
public class DealLock {
    public static void main(String[] args) {
        Object o1 = new Object();
        Object o2 = new Object();
        DealLockThreadOne t1 = new  DealLockThreadOne(o1,o2);
        DealLockThreadTwo t2 = new  DealLockThreadTwo(o1,o2);
        t1.start();
        t2.start();

    }
}
class DealLockThreadOne extends Thread{
    Object o1;
    Object o2;
    DealLockThreadOne(Object o1,Object o2){
       this.o1 = o1;
       this.o2 = o2;
    }

    @Override
    public void run() {
        synchronized (o1){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (o2){
               System.out.println("DealLockThreadOne线程被锁了，永远执行不到这里");
            }
        }
    }
}
class DealLockThreadTwo extends Thread{
    Object o1;
    Object o2;
    DealLockThreadTwo(Object o1,Object o2){
        this.o1 = o1;
        this.o2 = o2;
    }
    @Override
    public void run() {
         synchronized (o2){
             synchronized (o1){
                 System.out.println("DealLockThreadTwo线程被锁了，永远执行不到这里");
             }
         }
    }
}
